//用来显示和隐藏SPAN控件
function ShowAndHideSpan(spanName)
{
	//得到相应文本框值是否为空
	var txt=$(spanName).val();
	if(txt!="")
	{
		$(spanName).parent('DIV').parent('div').children('label').children('span').hide();
	}
	else
	{
		$(spanName).parent('DIV').parent('div').children('label').children('span').show();
	}	
};

jQuery(function () {
	var e_mail=0;   //e_mail是否合法的标志。如果为0表示不合法。
	var username=0;  //同上。
	//regBtn鼠标点击事件
	$('#regBtn').click(function(e) {
		var txtUser=$('#txtUser').val();
		var txtPwd=$('#txtPwd').val();
		var txtConfirm=$('#txtPwdConfirm').val();
		var txtTel=$('#txtTel').val();
		var txtEmail=$('#txtEmail').val();
		var txtAddr=$('#txtAddr').val();
		
		var txtUserKind=$('#selectUserSort').val();
		if(txtUser=="" || txtPwd=="" || txtConfirm=="" || txtTel=="" || txtEmail=="" || txtAddr=="")
		{
			alert('Please complete the information of the registration.');
		}
		else 
		{
			if(txtPwd!=txtConfirm)
			{
				alert('The passwords did not match, please re-enter。');
				$('#txtPwd').val("");
				$('#txtPwdConfirm').val("");
		    }
			else
			{
				if(e_mail==1&&username==1)
				{
					$.get("../../reg_ajax.ashx",{user:txtUser,pwd:txtPwd,tel:txtTel,
													 email:txtEmail,addr:txtAddr,time:Date()
					},function(data){
					var str=data;
					if(str=="1")
					{
						if(confirm('Registration information accepted, please return to the home page and login')==1)
						{
							window.location="index.aspx";
						}
						else 
						{
							$('#txtUser').val("");
							$('#txtPwd').val("");
							$('#txtPwdConfirm').val("");
							$('#txtTel').val("");
							$('#txtEmail').val("");
							$('#txtAddr').val("");
						}
					}
					else
					{
						alert('Registration information was not accepted, please register again.');
					}
					});
				}
				else 
				{
					alert('Please check if your User Name and Email Address accord with the regulation.');
				}
			}
		}
    });
	/////
	
	///用户名文本框失去焦点
	$('#txtUser').blur(function() {
		var name=$('#txtUser').val();
		if(name!="")
		{
			var regex=new RegExp("^[a-zA-Z][a-zA-Z0-9_]{4,15}$");
			var isMatch=regex.test(name);
			if(isMatch)
			{
				$('#userID').hide();
				$.get('../../regValidate.ashx',{user:name},function(data){
					if(data!=0)
					{
						$('#userID').text(" User Name already existed, please re-enter.");
						$('#userID').show();	
					}
					else
					{
						username=1;
						$('#userID').hide();
						ShowAndHideSpan("#txtUser");
					}
				});
			}
			else 
			{
				$('#userID').text("The User Name should be the combination of figures and letters.");
				$('#userID').show();
			}
		}
    });
	////
	
	///密码码框失去焦点
	$('#txtPwd').blur(function() {
		ShowAndHideSpan("#txtPwd");
    });
	////
		
	///确认密码框
	$('#txtPwdConfirm').blur(function() {
		ShowAndHideSpan("#txtPwdConfirm");
	});
	////
	
	$('#selectUserSort').change(function(e) {
        ShowAndHideSpan("#selectUserSort");
    });
	
	///
	$('#txtTel').blur(function() {
		ShowAndHideSpan("#txtTel");
    });
	////
	////Email
	$('#txtEmail').blur(function() {
		var email=$('#txtEmail').val();
		if(email!="")
		{
			var regex=new RegExp("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");
			var isMatch=regex.test(email);
			if(isMatch)
			{
				e_mail=1;
				$('#emailID').hide();
				ShowAndHideSpan("#txtEmail");
			}
			else 
			{
				$('#emailID').text("The Email form is officially unaccepted, please re-enter.");
				$('#emailID').show();
			}
		}
    });
    
    //Addr
     $('#txtAddr').blur(function() {
     	ShowAndHideSpan("#txtAddr");
    });
});